package com.game.fighting;

import com.almasb.fxgl.animation.Interpolators;
import com.almasb.fxgl.app.CursorInfo;
import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.SpawnData;
import com.almasb.fxgl.input.UserAction;
import com.almasb.fxgl.physics.CollisionHandler;
import com.almasb.fxgl.physics.HitBox;
import com.game.fighting.config.ArcheryType;
import com.game.fighting.facaory.ArcheryEntityFactory;
import com.game.fighting.component.BowComponent;
import com.game.fighting.socket.NetService;
import com.game.fighting.socket.SocketClient;
import javafx.scene.input.MouseButton;
import javafx.util.Duration;
import lombok.SneakyThrows;

import static com.almasb.fxgl.dsl.FXGL.*;

/**
 * Copyright (C), 2021-2041, chenc 豫R7HQ53
 *
 * @author: chenc
 * Date:     2023/12/4 20:24
 * Description:
 */
public class GameClient extends GameApplication {

    private Entity bow;
    private  String url = "ws://localhost:9090/test/websocket/game";

    @Override
    protected void initSettings(GameSettings settings) {
        settings.setWidth(800);
        settings.setHeight(600);
        settings.setDefaultCursor(new CursorInfo("cursorDefault.png", 0, 0));
    }

    @Override
    protected void onPreInit() {
        getSettings().setGlobalSoundVolume(0.08);
    }

    @SneakyThrows
    @Override
    protected void initGame() {
        //SocketClient socketClient = NetService.getNetClient(url);
        getGameWorld().addEntityFactory(new ArcheryEntityFactory());

        spawn("bg");
        bow = spawn("bow", 0, 220);
        spawn("target", 750, 260);
        spawn("shootBar", 35, 3);
    }

    @Override
    protected void initInput() {
        getInput().addAction(new UserAction("shoot") {
            @Override
            protected void onActionBegin() {
                spawn("click",getInput().getMousePositionUI().subtract(1120/7.0/2, 160/2.0) );
                BowComponent component = bow.getComponent(BowComponent.class);
                component.shoot();
            }
        }, MouseButton.PRIMARY);
    }

    @Override
    protected void initPhysics() {
        getPhysicsWorld().addCollisionHandler(new CollisionHandler(ArcheryType.TARGET, ArcheryType.ARROW) {
            @Override
            protected void onHitBoxTrigger(Entity target, Entity arrow, HitBox targetBox, HitBox arrowBox) {
                play("hit.wav");
                spawn("star",
                        arrow.getRightX() - 2138 / 8.0 / 2.0, arrow.getY() + 1 - 276 / 2.0);

                spawnWithScale("score",
                        new SpawnData(arrow.getRightX() - 11, arrow.getY() - 14)
                                .put("area", targetBox.getName()),
                        Duration.seconds(0.8),
                        Interpolators.BOUNCE.EASE_OUT());
                arrow.removeFromWorld();

            }

        });
    }

    public static void main(String[] args) throws Exception {

        launch(args);

    }


}
